package com.atguigu.gmall.common.config.utils;

import com.atguigu.gmall.common.constant.RedisConst;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-16 21:54
 **/
public class UserAuthUtil {
    /**
     * 获取老请求：通过ThreadLocal 线程绑定机制
     *
     * @return
     */
    public static HttpServletRequest request() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // 异步情况下 没有给异步线程共享数据导致为null
        if (attributes != null) {
            return attributes.getRequest();
        }
        return null;
    }

    public static String getUserIdHeader() {
        return request().getHeader(RedisConst.USER_ID_HEADER);
    }

    public static String getUserTempIdHeader() {
        return request().getHeader(RedisConst.USER_TEMP_ID_HEADER);
    }
}
